//
//  GoEvaluateViewController.m
//  KiklinkApp
//
//  Created by LJC on 14/10/27.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "GoEvaluateViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"

#import "API.h"

@interface GoEvaluateViewController ()
{
    ImagesManager *imagesManager;
    NSInteger grade;
}
@end

@implementation GoEvaluateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"马上评价";
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    grade = 10;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
    
    
    //BundleImages
    imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //star
    [self.star1 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star2 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star3 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star4 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star5 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    
    [self.star1 addTarget:self action:@selector(star1Action) forControlEvents:UIControlEventTouchUpInside];
    [self.star2 addTarget:self action:@selector(star2Action) forControlEvents:UIControlEventTouchUpInside];
    [self.star3 addTarget:self action:@selector(star3Action) forControlEvents:UIControlEventTouchUpInside];
    [self.star4 addTarget:self action:@selector(star4Action) forControlEvents:UIControlEventTouchUpInside];
    [self.star5 addTarget:self action:@selector(star5Action) forControlEvents:UIControlEventTouchUpInside];
    
    
    //evaluateText
    self.evaluateText.delegate = self;
    self.evaluateText.layer.borderWidth = 0.5;
    self.evaluateText.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:0.5].CGColor;
    [self.evaluateText.layer setMasksToBounds:YES];
    [self.evaluateText.layer setCornerRadius:5];
    
    
    //evaluateBtn
    [self.evaluateBtn.layer setCornerRadius:5];
    [self.evaluateBtn addTarget:self action:@selector(evaluateAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma mark - star
- (void)star1Action
{
    [self.star1 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star2 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    [self.star3 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    [self.star4 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    [self.star5 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    self.gradeLabel.text = @"1星";
    grade = 2;
}
- (void)star2Action
{
    [self.star1 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star2 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star3 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    [self.star4 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    [self.star5 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    self.gradeLabel.text = @"2星";
    grade = 4;
}
- (void)star3Action
{
    [self.star1 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star2 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star3 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star4 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    [self.star5 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    self.gradeLabel.text = @"3星";
    grade = 6;
}
- (void)star4Action
{
    [self.star1 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star2 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star3 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star4 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star5 setBackgroundImage:[imagesManager grayStar] forState:UIControlStateNormal];
    self.gradeLabel.text = @"4星";
    grade = 8;
}
- (void)star5Action
{
    [self.star1 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star2 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star3 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star4 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    [self.star5 setBackgroundImage:[imagesManager yellowStar] forState:UIControlStateNormal];
    self.gradeLabel.text = @"5星";
    grade = 10;
}
#pragma mark - star End

#pragma mark - textView delegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        self.evaluatePlaceHolder.hidden = NO;
    } else {
        self.evaluatePlaceHolder.hidden = YES;
    }
}
#pragma mark - textView delegate End

#pragma mark - evaluateAction
- (void)evaluateAction
{
    NSNumber *gradeNumber = [NSNumber numberWithInteger:grade];
    [API addCommentWithProductId:[NSNumber numberWithInteger:self.productID] comment:self.evaluateText.text pointDescriptionMath:gradeNumber pointProfessional:gradeNumber pointServiceAttitude:gradeNumber pointAppointmentSuccessful:gradeNumber callback:^(int stateCode, NSString *word, id content) {}];
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - evaluateAction End

#pragma mark - tapAction
- (void)tapAction
{
    [self.evaluateText resignFirstResponder];
}
#pragma mark - tapAction End

@end
